http://ithelp.ithome.com.tw/ironman5/player/seanamph/tech/1
今天吵完架後已經晚上11點了,要try code的時間恐怕不夠,所以整理一下這些和IO有關的code,也好釐清一些觀念,所以今晚也沒有什麼test的圖.......
資料讀取的地方有三種local roaming temporary
http://msdn.microsoft.com/zh-tw/library/windows/apps/hh464917
這裡講得很仔細
local 本機 只存在目前裝置上的持續性資料。
roaming 漫遊 存在使用者安裝了應用程式的所有裝置上的資料。
temporary 暫時 系統隨時可以移除的資料。
所以看一下 Windows.Storage.ApplicationData 這個class
http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.aspx#properties
這是local
var applicationData = Windows.Storage.ApplicationData.current;
var localSettings = applicationData.localSettings;
var localFolder = applicationData.localFolder;
然後這樣可以存取一筆 localSettings資料
localSettings.values["exampleSetting"] = "Hello Windows";
var value = localSettings.values["exampleSetting"];
還有localFolder
function writeTimestamp() {
localFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
.then(function (sampleFile) {
var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
var timestamp = formatter.format(new Date());
return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
}).done(function () {
});
}
function readTimestamp() {
localFolder.getFileAsync("dataFile.txt")
.then(function (sampleFile) {
return Windows.Storage.FileIO.readTextAsync(sampleFile);
}).done(function (timestamp) {
// 讀取檔案後
}, function () {
// 檔案不存在
});
}
好像有點複雜又不會太複雜
這是 roaming,存取方法也差不多
var applicationData = Windows.Storage.ApplicationData.current;
var roamingSettings = applicationData.roamingSettings;
var roamingFolder = applicationData.roamingFolder;
//讀取設定
roamingSettings.values["exampleSetting"] = "哈囉沃德";
var value = roamingSettings.values["exampleSetting"];
// 寫入檔案
function writeTimestamp() {
roamingFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
.then(function (sampleFile) {
var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
var timestamp = formatter.format(new Date());
return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
}).done(function () {
});
}
// 讀取檔案
function readTimestamp() {
roamingFolder.getFileAsync("dataFile.txt")
.then(function (sampleFile) {
return Windows.Storage.FileIO.readTextAsync(sampleFile);
}).done(function (timestamp) {
// 讀取檔案後
}, function () {
// 檔案不存在
});
}
這是temporary
var applicationData = Windows.Storage.ApplicationData.current;
var temporaryFolder = applicationData.temporaryFolder;
function writeTimestamp() {
temporaryFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
.then(function (sampleFile) {
var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
var timestamp = formatter.format(new Date());
return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
}).done(function () {
});
}
function readTimestamp() {
temporaryFolder.getFileAsync("dataFile.txt")
.then(function (sampleFile) {
return Windows.Storage.FileIO.readTextAsync(sampleFile);
}).done(function (timestamp) {
// 讀取檔案後
}, function () {
// 檔案不存在
});
}
再看一下Windows.Storage 這個namespace
http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.aspx
其中FileIO class,是剛看到的存取檔案的方法
http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.fileio.aspx
PathIO class 和 .net 的 path 很像
http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.pathio.aspx
Windows.Storage.StorageFolder Class 和 .net 的 directory 很像
http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefolder.aspx#properties
StorageFile class 和 .net 的 file 很像
http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.aspx
KnownFolders class 已知的目錄
http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.knownfolders.aspx
DocumentsLibrary 文件目錄
HomeGroup 使用者目錄
MediaServerDevices 媒體
MusicLibrary 音樂目錄
PicturesLibrary 圖片
RemovableDevices 可移除裝置
VideosLibrary 影片
好吧....又混了一天...
繼續趕code Or22